package ar.edu.itba.it.paw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ar.edu.itba.it.paw.dao.UserDAO;
import ar.edu.itba.it.paw.domain.ConnectionManager;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.domain.exceptions.DatabaseException;


@Repository
public class DatabaseUserDAO implements UserDAO{
	
	private ConnectionManager manager;
	
	@Autowired
	public DatabaseUserDAO(ConnectionManager manager){
		this.manager = manager;
	}
	
	@Override
	public User getUser(String username) {
		Connection conn = manager.getConnection();
		PreparedStatement stmt;
		User usuario = null;
		
		try{
			stmt = conn.prepareStatement("select * from \"Usuario\" where \"userName\" = ?");
			stmt.setString(1, username);
			ResultSet cur = stmt.executeQuery();
			if(cur.next()){
			usuario = new User(cur.getString("userName"),cur.getString("password"), cur.getString("nombre"), cur.getString("apellido"), cur.getString("mail"),cur.getInt("userId"),cur.getBoolean("admin"));
			} else {
				manager.closeConnection();
				return null;
			}
		}catch(SQLException e){
			throw new DatabaseException();
		}
		manager.closeConnection();
	
		return usuario;
		
	}
	
	@Override
	public boolean existsMail(User user) {
		Connection conn = manager.getConnection();
		PreparedStatement stmt;
		
		try{
			stmt = conn.prepareStatement("select * from \"Usuario\" where mail = ?");
			stmt.setString(1, user.getMail());
			ResultSet cur = stmt.executeQuery();
			if(cur.next()){
				manager.closeConnection();
				return true;
			}
		}catch(SQLException e){
			throw new DatabaseException();
		}
		manager.closeConnection();
		return false;
	}
	
	
	public boolean existsUser(User user){
		if(this.getUser(user.getUsername()) == null && this.existsMail(user) == false){
			return false;
		}
		return true;
	}
	
	
	public User getUser(int user_id){
		Connection conn = manager.getConnection();
		PreparedStatement stmt;
		User usuario = null;
		
		try{
			stmt = conn.prepareStatement("select * from \"Usuario\" where \"userId\" = ?");
			stmt.setInt(1, user_id);
			ResultSet cur = stmt.executeQuery();
			if(cur.next()){
			usuario = new User(cur.getString("userName"),cur.getString("password"), cur.getString("nombre"), cur.getString("apellido"), cur.getString("mail"),cur.getInt("userId"),cur.getBoolean("admin"));
			} else {
				manager.closeConnection();
				return null;
			}
		}catch(SQLException e){
			throw new DatabaseException();
		}
		manager.closeConnection();
	
		return usuario;
	}

	@Override
	public void addUser(User usuario) {
		Connection conn = manager.getConnection();
		PreparedStatement stm;
		try {
			stm = conn.prepareStatement("INSERT INTO \"Usuario\" (\"userName\", nombre, apellido, \"password\", mail) VALUES (?, ?, ?, ?, ?)"); 
			stm.setString(1, usuario.getUsername());		
			stm.setString(2, usuario.getFirst_name());
			stm.setString(3, usuario.getLast_name());
			stm.setString(4, usuario.getPassword());
			stm.setString(5, usuario.getMail());
			stm.execute();
		} catch (SQLException e) {
			throw new DatabaseException();
		}
		manager.closeConnection();
	}

	@Override
	public void updateUser(User user) {
		Connection conn = manager.getConnection();
		
		PreparedStatement stmt;
		
		try{
			stmt = conn.prepareStatement("update \"Usuario\" set nombre = ?, apellido = ?, password = ? where \"userId\" = ? ");
			stmt.setString(1, user.getFirst_name());
			stmt.setString(2, user.getLast_name());
			stmt.setString(3, user.getPassword());
			stmt.setInt(4, user.getId());
			stmt.execute();
		}catch(SQLException e){
			e.printStackTrace();
			throw new DatabaseException();
		}
		manager.closeConnection();
	}
}
